<?php

/**
 * Subclass for representing a row from the 'text_module' table.
 *
 * 
 *
 * @package lib.model
 */ 
class TextModule extends BaseTextModule
{

	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		$this->setCulture(sfContext::getInstance()->getUser()->getCulture());
		parent::hydrate($rs, $startcol);
	}

    /**
    *   Creates new text module, or returns text module, which user created, but did not approve
    *   @param $user integer ID of user
    *   @return TextModule created or found from unused
    */
    public static function getNewOrUnused($userId)
    {
        $obj = null;

        if( $userId > 0 )
        {
            $c = new Criteria();
            $c->add(TextModulePeer::USER_ID, $userId);
            $c->add(TextModulePeer::STATUS_ID, MyConstants::STATUS_DELETED);
            $obj = TextModulePeer::doSelectOne($c);

            if( !empty($obj) )
            {
                $obj = $obj;
                $obj->setTextLayoutId(0);
                //$obj->setGalleryId(0);
                $obj->setName('');
                $obj->setCreatedAt(time(0));
            }
        }

        if( $obj == null )
        {
            $obj = new TextModule();
            $obj->setStatusId(MyConstants::STATUS_DELETED);
            $obj->setUserId($userId);
        }

        return $obj;
    }

    public function __toString()
    {
        sfLoader::loadHelpers(array('Tag', 'Asset'));
        //debugvar($this);
        //Setting simple string variable
        $sHtml = '';

        //Getting text layout id from $this
        $textLayoutId = $this->getTextLayoutId() ? $this->getTextLayoutId() : 1;
        
        //Creating new text layout instance and retrieving one from db by $textLayoutId
        $layout = new TextLayout();
        $layout = TextLayoutPeer::retrieveByPk($textLayoutId);

        // TODO: check!!!
        //Getting text from $this
        $text = $this->getText();
        //Getting layout preset
        $layoutPreset = $layout->getLayout();

        //Replacing %%TEXT%% from layout preset with text from $this
        $finalText = str_replace('%%TEXT%%', $text, $layoutPreset);

        // TODO: check!!!
        //Getting image related to object $this
        $imageObject = Files::getFirst($this->getId());
        //debugvar($imageObject);

        //Declaring final image path and name
        if( empty($imageObject) )
        {
            $image = '';
        }else
        {
            //Replacing %%IMAGE%% with the final image tag
            $image = '<img src="http://' . sfConfig::get('app_website') . '/' . sfConfig::get('sf_web_dir_name') . '/' . sfConfig::get('app_images_dir') . '/gallery/tiny/' . $imageObject->getUname() . '" alt="aaa" />';
        }

        $finalText = str_replace('%%IMAGE%%', $image, $finalText);

        //Adding parsed text to primary string variable
        $sHtml .= $finalText;
        return $sHtml;
    }


    public static function getById($id)
    {
        $obj = TextModulePeer::retrieveByPk($id);
        if( empty($obj) )
            $obj = new TextModule();

        return $obj;
    }

    public function delete($con = null)
    {
        Files::deleteByObjId($this->getId(), true, MyConstants::TYPE_TEXTMODULE);
        parent::delete($con);
    }

}
